var addHandler = function(ev){
	var selNode;
	selNode = Ext.getCmp('companyTreePanel').getSelectionModel().getSelectedNode();
	if (selNode)
		createAddForm(selNode);
}

var editHandler = function(ev){
	var selNode;
	selNode = Ext.getCmp('companyTreePanel').getSelectionModel().getSelectedNode();
	if (selNode)
		createEditForm(selNode);
}

var deleteHandler = function(ev){
	var selNode;
	selNode = Ext.getCmp('companyTreePanel').getSelectionModel().getSelectedNode();
	if (selNode)
		Ext.MessageBox.confirm(
				'Are you sure?',
				'Please confirm the deletion of ' + selNode.attributes.text,
				onConfirmDelete
		)
}

var buildCtxMenu = function() {
	return new Ext.menu.Menu({
		items: [
		        {
		        	itemId  : 'add',
		        	iconCls : 'add',
		        	handler : addHandler
		        },
		        '-',
		        {
		        	itemId  : 'edit',
		        	iconCls : 'edit',
		        	handler : editHandler
		        },
		        {
		        	itemId  : 'delete',
		        	iconCls : 'delete',
		        	handler : deleteHandler
		        },
		        ]
	});
}

var onCtxMenu = function(node, evtObj) {
	node.select();
	evtObj.preventDefault();
	evtObj.stopEvent();

	if (! this.ctxMenu) {
		this.ctxMenu = buildCtxMenu();
	}

	var ctxMenu    = this.ctxMenu;
	var addItem    = ctxMenu.getComponent('add');
	var editItem   = ctxMenu.getComponent('edit');
	var deleteItem = ctxMenu.getComponent('delete');

	if (node.id =='CompanyTreeRoot') {
		addItem.setText('Add Division');
		editItem.setText('Can\'t edit a company');
		deleteItem.setText('Can\'t delete a company');

		addItem.enable();
		deleteItem.disable();
		editItem.disable();
	}
	else if (node.id.match('Division')) {
		addItem.setText('Add Project');
		editItem.setText('Edit Division');
		deleteItem.setText('Delete Division');

		addItem.enable();
		editItem.enable();
		deleteItem.enable();
	}
	else if (node.id.match('Project')) {
		addItem.setText('Add Person');
		editItem.setText('Edit Project');
		deleteItem.setText('Delete Project');

		addItem.enable();
		editItem.enable();
		deleteItem.enable();
	}
	else if (node.leaf) {
		addItem.setText('No addings for person');
		editItem.setText('Edit Person');
		deleteItem.setText('Delete Person');

		addItem.disable();
		editItem.enable();
		deleteItem.enable();
	}

	ctxMenu.showAt(evtObj.getXY()) ;
}